import React, { Component } from "react";
import axios from "axios";
import qs from "qs";
import { Tabs, message, List } from "antd";
import { createBrowserHistory } from "history";
import "../mock/wmeetingdata";
const { TabPane } = Tabs;
export default class Meetings extends Component {
  constructor(props) {
    super(props);
    this.TodayDate = "";
    this.TomorrowDate = "";
    this.MonDate = "";
    this.SunDate = "";
    this.state = {
      TodayData: [],
      TomorrowData: [],
    };
  }

  getDate() {
    const toString = (data) => {
      return (
        data.getFullYear() +
        "-" +
        (data.getMonth() < 9
          ? "0" + Number(data.getMonth() + 1)
          : data.getMonth() + 1) +
        "-" +
        data.getDate()
      );
    };
    var now = new Date();
    var nowTime = now.getTime();
    var day = now.getDay() || 7;
    var oneDayTime = 24 * 60 * 60 * 1000;
    var MondayTime = nowTime - (day - 1) * oneDayTime;
    var SundayTime = nowTime + (7 - day) * oneDayTime;
    this.MonDate = toString(new Date(MondayTime));
    this.SunDate = toString(new Date(SundayTime));
    this.TodayDate = toString(now);
    this.TomorrowDate = toString(new Date(nowTime + 24 * 60 * 60 * 1000));
  }

  callback(key) {
    // 切换tab
  }
  getToday() {
    axios
      .post(
        "fetchweekmeetingbyparticipatorid.rst",
        qs.stringify({
          startDate: this.TodayDate,
          endDate: this.TodayDate,
        })
      )
      .then((res) => {
        let response = res.data;
        if (response.resultCode === 0) {
          let data = response.result.data;
          console.log(data);
          this.setState({
            TodayData: data.map((data, index) => {
              return {
                key: data.id,
                meetingDate: data.meetingDate,
                meetingTime: data.meetingTime,
                meetingAddress: data.locale,
                title: data.theme,
                host: data.host + " ",
                participator: data.participator.split(","),
                remindTime: data.remindTime,
                meetingRoom: data.meetingRoom,
              };
            }),
          });
        } else {
          // alert('数据请求失败')
          message.error("数据请求失败");
        }
      })
      .catch((error) => {
        console.log(error);
        // alert('数据请求失败')
        message.error("数据请求失败");
      });
  }
  getTomorrow() {
    axios
      .post(
        "fetchweekmeetingbyparticipatorid.rst",
        qs.stringify({
          startDate: this.TomorrowDate,
          endDate: this.TomorrowDate,
        })
      )
      .then((res) => {
        let response = res.data;
        if (response.resultCode === 0) {
          let data = response.result.data;
          console.log(data);
          this.setState({
            TomorrowData: data.map((data, index) => {
              return {
                key: data.id,
                meetingDate: data.meetingDate,
                meetingTime: data.meetingTime,
                meetingAddress: data.locale,
                title: data.theme,
                host: data.host + " ",
                participator: data.participator.split(","),
                remindTime: data.remindTime,
                meetingRoom: data.meetingRoom,
              };
            }),
          });
        } else {
          // alert('数据请求失败')
          message.error("数据请求失败");
        }
      })
      .catch((error) => {
        console.log(error);
        // alert('数据请求失败')
        message.error("数据请求失败");
      });
  }

  componentDidMount() {
    this.getDate();
    this.getToday();
    this.getTomorrow();
  }
  weekMeeting() {
    const history = createBrowserHistory();
    history.push("./#/WWeekMeetings");
    history.go();
  }
  render() {
    // const { TabPane } = this.Tabs;
    return (
      <div className="card-container">
        <div onClick={this.weekMeeting} className="go-week">
          本周会议安排&gt;
        </div>
        <Tabs defaultActiveKey="1" onChange={this.callback}>
          <TabPane tab="今日会议" key="1">
            <List
              itemLayout="horizontal"
              dataSource={this.state.TodayData}
              renderItem={(item) => (
                <List.Item>
                  <List.Item.Meta
                    title={<div>{item.title}</div>}
                    description={
                      <div className="des-text-parent">
                        <span className="des-text">{item.meetingDate}</span>
                        <span className="des-text">{item.meetingTime}</span>
                        <span className="des-text">{item.meetingAddress}</span>
                      </div>
                    }
                  />
                </List.Item>
              )}
            />
          </TabPane>
          <TabPane tab="明日会议" key="2">
            <List
              itemLayout="horizontal"
              dataSource={this.state.TomorrowData}
              renderItem={(item) => (
                <List.Item>
                  <List.Item.Meta
                    title={<div>{item.title}</div>}
                    description={
                      <div className="des-text-parent">
                        <span className="des-text">{item.meetingDate}</span>
                        <span className="des-text">{item.meetingTime}</span>
                        <span className="des-text">{item.meetingAddress}</span>
                      </div>
                    }
                  />
                </List.Item>
              )}
            />
          </TabPane>
        </Tabs>
      </div>
    );
  }
}
